package com.zyh.controller;

import com.zyh.pojo.Post;
import com.zyh.pojo.User;
import com.zyh.service.FollowService;
import com.zyh.service.PostService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class ProfileController {
    @Autowired
    PostService postService;
    @Autowired
    FollowService followService;
    /**
     * 个人主页
     * @param username
     * @param model
     * @return
     */
    @GetMapping("/{username}")
    public String profile(@PathVariable String username, Model model){
        model.addAttribute("username",username);
        String loginUserName= (String) SecurityUtils.getSubject().getPrincipal();
        List<Post> posts=postService.findPostsByUserName(username);
        model.addAttribute("posts",posts);
        //获取粉丝
        List<User> followers=followService.followers(username);
        //获取关注者
        List<User> followings=followService.followings(username);
        model.addAttribute("followers",followers);
        model.addAttribute("followings",followings);
        if(!loginUserName.equals(username))//访问别人的主页
        {
            model.addAttribute("isfollowing",followService.judgeFollow(loginUserName,username));
        }
        return "profile";
    }

    /**
     * follower关注befollwer
     * @param follower
     * @param befollwer
     * @return
     */
    @GetMapping("{follower}/follow/{befollower}")
    public String follow(@PathVariable String follower,@PathVariable String befollower){
        followService.follow(follower, befollower);
        return "redirect:/"+befollower;
    }

    /**
     * follower取消关注befollwer
     * @param follower
     * @param befollower
     * @return
     */
    @GetMapping("{follower}/unfollow/{befollower}")
    public String unfollow(@PathVariable String follower,@PathVariable String befollower){
        followService.unfollow(follower, befollower);
        return "redirect:/"+befollower;
    }


}
